home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / uip / snd / alias.c next >
Encoding:
C/C++ Source or Header  |  1987-03-12  |  5.6 KB  |  258 lines

  1. #include <stdio.h>
  2. #include "../h/util.h"
  3. #include "../h/mmdf.h"
  4.  
  5. #define BSIZE        1024
  6.  
  7. struct ALIAS {
  8.     char *key;
  9.     int cntr;
  10.     char *names;
  11.     struct ALIAS *next_int;
  12. } *als, alias_ptr, *old_als;
  13.  
  14. int aflag;
  15. char *adrptr;
  16. char *malloc();
  17.  
  18. aliasinit( aliasfile )
  19. char *aliasfile;
  20. {
  21.     char alsfilename[128];
  22.     FILE *afd;
  23.     char *akey, *list;
  24.     char tmpbuf[BSIZE];
  25.     aflag = 1;
  26.  
  27.     strcpy( alsfilename, aliasfile );
  28.         /*
  29.         **  Open the alias file and store the keys and lists
  30.         **  in core
  31.         */
  32.  
  33.     afd = fopen( alsfilename, "r" );
  34.     if( afd <= NULL ) {
  35.         aflag = 0;
  36.         perror(" Can't open alias file ");
  37.     }
  38.     als = &alias_ptr;
  39.     old_als = NULL;
  40.     while(1) {
  41.         if(fgets(tmpbuf, sizeof(tmpbuf), afd) != NULL) {
  42.             get_key(tmpbuf, &akey, &list);
  43.             if( old_als > 0 ) {
  44.                 if(( als = (struct ALIAS *) malloc(sizeof(alias_ptr))) <= 0) {
  45.                     perror( "MALLOC ERROR!");
  46.                     break;
  47.                 }
  48.                 old_als->next_int = als;
  49.             }
  50.             als->key = akey;
  51.             als->cntr = 0;
  52.             als->names = list;
  53.             old_als = als;
  54.         }
  55.         else 
  56.         {
  57.             old_als = NULL;
  58.             break;
  59.         }
  60.     }
  61. }
  62.  
  63. int linelen, destlen, addrlen;
  64.  
  65. aliasmap(dest, src, thehost)
  66. char *dest, *src, thehost[];
  67. {
  68.     char addr[BSIZE],
  69.      name[ADDRSIZE],
  70.      address[ADDRSIZE],
  71.      temphost[FILNSIZE],
  72.      defhost[FILNSIZE],
  73.      hostname[FILNSIZE];
  74.     char *ptr;
  75.     char *sadrptr;
  76.     int sub_flag;
  77.  
  78.     if (thehost != 0)                /* save official version of default   */
  79.     strcpy (defhost, thehost);
  80.  
  81.     for (ptr = dest, linelen = 0; *ptr != '\0'; ptr++, linelen++)
  82.     if (*ptr == '\n')         /* get length of last line of header  */
  83.         linelen = -1;         /* make it zero for next character    */
  84.     destlen = ptr - dest;
  85.  
  86.     for (adrptr = src;; )         /* adrptr is state variable, used in  */
  87.     {                             /*   next_address                     */
  88.     switch (next_address (addr))
  89.     {
  90.         case -1:              /* all done                           */
  91.  
  92.         /* clean markers in aliases structure */
  93.         als = &alias_ptr;
  94.         while(als != NULL ){
  95.             als->cntr = 0;
  96.             als = als->next_int;
  97.         }
  98.         return;
  99.  
  100.         case 0:               /* empty address */
  101.         continue;
  102.     }
  103.  
  104.     name[0] = address[0] = temphost[0] = '\0';
  105.  
  106.     parsadr (addr, name, address, temphost);
  107.     if (address[0] == '\0')
  108.     {                         /* change the default host */
  109.         if (temphost[0] != '\0')
  110.         strcpy (defhost, temphost);
  111.         continue;
  112.     }
  113.     if( aflag && temphost[0]==0){
  114.         als = &alias_ptr;
  115.         sub_flag = 0;
  116.         while( als != NULL ){
  117.             if( lexequ(address, als->key) && als->cntr == 0 ){
  118.                 als->cntr++;
  119.                 sadrptr = adrptr;
  120.                 aliasmap(dest, als->names, thehost);
  121.                 adrptr = sadrptr;
  122.                 sub_flag = 1;
  123.                 break;
  124.             }
  125.             else
  126.                 if( lexequ(address, als->key) && als->cntr != 0 ){
  127.                     sub_flag = 1;
  128.                     break;
  129.                 }
  130.             als = als->next_int;
  131.         }
  132.         if(sub_flag == 1)
  133.             continue;
  134.     }
  135.     if (temphost[0] == '\0')
  136.         strcpy (hostname, defhost);
  137.     else
  138.         strcpy (hostname, temphost);
  139.  
  140.     if (name[0] != '\0')      /* put into canonical form            */
  141.     {                         /* name <mailbox@host>                */
  142.         for (ptr = hostname; !isnull (*ptr); ptr++)
  143.         *ptr = uptolow (*ptr);
  144.                   /* force hostname to lower            */
  145.         for (ptr = name; ; ptr++)
  146.         {                     /* is quoting needed for specials?    */
  147.         switch (*ptr)
  148.         {
  149.             default:
  150.             continue;
  151.  
  152.             case '\0':    /* nope                               */
  153.             sprintf (addr, "%s <%s@%s>",
  154.                 name, address, hostname);
  155.             goto doaddr;
  156.  
  157.             case '<':     /* quoting needed                     */
  158.             case '>':
  159.             case '@':
  160.             case ',':
  161.             case ';':
  162.             case ':':
  163.             sprintf (addr, "\"%s\" <%s@%s>",
  164.                     name, address, hostname);
  165.             goto doaddr;
  166.         }
  167.         }
  168.     }
  169.     else
  170.     {
  171.         sprintf (addr, "%s@%s", address, hostname);
  172.     }
  173.  
  174. doaddr:
  175.     addrlen = strlen (addr);
  176.  
  177.     if ((destlen + addrlen) < (BSIZE - 4))
  178.     {                         /* it will fit into dest buffer       */
  179.         if (destlen == 0)
  180.         strcpy (dest, addr);
  181.         else          /* monitor line length                */
  182.         {
  183.         if ((linelen + addrlen) < 70)
  184.             sprintf (&dest[destlen], ", %s", addr);
  185.         else
  186.         {
  187.             sprintf (&dest[destlen], ",\n %s", addr);
  188.             linelen = 0;
  189.             destlen += 1;
  190.         }
  191.         destlen += 2;
  192.         linelen += 2;
  193.         }
  194.         destlen += addrlen;
  195.         linelen += addrlen;
  196.     }
  197.         else
  198.         {
  199.             fprintf(stderr,"WARNING: Header line exceeds buffer size!\n");
  200.             fprintf(stderr,"\tIt was truncated to legal length.\n");
  201.             break;
  202.         }
  203.     }
  204. }
  205.  
  206. get_key( s, okey, olist )
  207. char *s, **okey, **olist;
  208. {
  209.     char tmps[512];
  210.     char *ptmps;
  211.     int len_list;
  212. /*
  213. **  This routine takes the input string s and divides it into two parts,
  214. **  the first of which consists of the first word in the original string.
  215. **  It is returned in okey. The second part is a list of the remaining names.
  216. **  They are returned in the olist. Malloc is used to allocate the necessary
  217. **  storage for the sublists.
  218. */
  219.  
  220.     /* throw away initial white space */
  221.  
  222.     while( *s == ' ' || *s == '\t' )
  223.         *s++;
  224.  
  225.     /* get the first word. It may be terminated by spaces, tabs, commas, */
  226.     /* newlines or end-of-file                          */
  227.  
  228.     ptmps = tmps;
  229.     while( *s != ' ' && *s != '\t' && *s != ',' && *s != '\n' && *s != EOF){
  230.         *ptmps++ = *s++;
  231.     }
  232.     *ptmps = 0;
  233.     len_list = strlen(tmps);
  234.     if(( *okey = malloc( (unsigned) (len_list) + 1 )) <= 0 ) {
  235.         perror("MALLOC ERROR!");
  236.         return(-1);
  237.     }
  238.  
  239.     strcpy( *okey, tmps );
  240.  
  241.     /* and now the rest of the list */
  242.  
  243.     ptmps = s;
  244.     while( *s != '\n' && *s != EOF && *s != '\0' )
  245.         *s++;
  246.     *s = 0;
  247.     len_list = strlen( ptmps );
  248.     if(len_list <= 0 )
  249.         return( -1 ); /* no list - ERROR - ERROR */
  250.     if((*olist = malloc( (unsigned) (len_list) +1 )) <= 0) {
  251.         perror("MALLOC ERROR!");
  252.         return( -1 );
  253.     }
  254.  
  255.     strcpy( *olist, ptmps );
  256.     return(0);
  257. }
  258.